package com.jt.www.remote;

import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

//业务管理认领远程
@FeignClient(value = "sttl-man", url = "${remote.sttl_man_url}",
        configuration = FeignConfiguration.class, fallbackFactory = SttlManClient.FeignClientFallbackFactory.class)
public interface SttlManClient {

    /**
     *渠道认领保单查询接口
     * @param policy_no  保单号
     * @param org_code 机构编码: 多个以 ; 分割
     * @param systemcode 归属系统: 1 - 门户 2-中交 3卫生渠道、4安全渠道、5特设渠道、6旅游渠道、7教育渠道
     * @return
     * @throws Exception
     * 必传项为 保单号  机构编码 部门编码  归属系统
     */
    @GetMapping(value = "/Mhds/mhdsDitchSelectByPolicyNo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String,Object> mhdsDitchSelectByPolicyNo(
                                                 @RequestParam(value = "policy_no") String policy_no,
                                                 @RequestParam(value = "org_code") String org_code,
                                                 @RequestParam(value = "systemcode") String systemcode) throws Exception;

    /**
     * 渠道认领保单接口
     * @param operparam
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/Mhds/mhdsDitchClaim", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String,Object> mhdsDitchClaim(List<Map<String, String>> operparam)throws Exception;

    /**
     * 渠道认领保单退回接口
     * @param operparam
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/Mhds/mhdsDitchReturn", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String,Object> mhdsDitchReturn(@RequestParam("operparam") List<Map<String, String>> operparam)throws Exception;

    /**
     * 获取业管跨期区间
     * @param today
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/Mhds/getIntertemporal", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    Map<String,String> getIntertemporal(@RequestParam("today") Date today)throws Exception;


    @Component
    class FeignClientFallbackFactory implements FallbackFactory<SttlManClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public SttlManClient create(Throwable cause) {
            return new SttlManClient() {
                @Override
                public Map<String, Object> mhdsDitchSelectByPolicyNo(String policy_no, String org_code,  String systemcode) throws Exception {
                    log.error("渠道认领保单查询接口,fallback reason:",cause);
                    return null;
                }

                @Override
                public Map<String,Object> mhdsDitchClaim(List<Map<String, String>> operparam) throws Exception {
                    log.error("保单认领接口,fallback reason:",cause);
                    return null;
                }

                @Override
                public Map<String,Object> mhdsDitchReturn(List<Map<String, String>> operparam) throws Exception {
                    log.error("保单退回接口,fallback reason:",cause);
                    return null;
                }

                @Override
                public Map<String, String> getIntertemporal(Date today) throws Exception {
                    log.error("获取业管跨期日期失败,fallback reason:",cause);
                    return null;
                }
            };
        }
    }
}
